﻿package com.rbac.system.controller;

import com.rbac.system.common.Result;
import com.rbac.system.service.CaptchaService;
import com.rbac.system.vo.CaptchaVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 验证码控制器
 */
@RestController
@RequestMapping("/api/captcha")
@CrossOrigin
public class CaptchaController {
    
    @Autowired
    private CaptchaService captchaService;
    
    /**
     * 生成验证码
     */
    @GetMapping("/generate")
    public Result<CaptchaVO> generateCaptcha(@RequestParam(defaultValue = "image") String type) {
        switch (type) {
            case "image":
                return captchaService.generateImageCaptcha();
            case "math":
                return captchaService.generateMathCaptcha();
            case "slider":
                return captchaService.generateSliderCaptcha();
            default:
                return captchaService.generateImageCaptcha();
        }
    }
    
    /**
     * 验证验证码
     */
    @PostMapping("/verify")
    public Result<Void> verifyCaptcha(@RequestBody Map<String, String> request) {
        String captchaId = request.get("captchaId");
        String userInput = request.get("userInput");
        
        if (captchaId == null || captchaId.trim().isEmpty()) {
            return Result.error("验证码ID不能为空");
        }
        if (userInput == null || userInput.trim().isEmpty()) {
            return Result.error("验证码不能为空");
        }
        
        return captchaService.verifyCaptcha(captchaId.trim(), userInput.trim());
    }
    
    /**
     * 验证滑块验证码
     */
    @PostMapping("/verify-slider")
    public Result<Void> verifySliderCaptcha(@RequestBody Map<String, Object> request) {
        String captchaId = (String) request.get("captchaId");
        Integer x = (Integer) request.get("x");
        
        if (captchaId == null || captchaId.trim().isEmpty()) {
            return Result.error("验证码ID不能为空");
        }
        if (x == null) {
            return Result.error("滑块位置不能为空");
        }
        
        return captchaService.verifySliderCaptcha(captchaId.trim(), x);
    }
    
    /**
     * 检查是否需要验证码
     */
    @GetMapping("/required")
    public Result<Map<String, Object>> isCaptchaRequired(@RequestParam String type) {
        boolean required = captchaService.isCaptchaRequired(type);
        String captchaType = captchaService.getCaptchaType();
        
        return Result.success(Map.of(
            "required", required,
            "type", captchaType
        ));
    }
    
    /**
     * 刷新验证码
     */
    @PostMapping("/refresh")
    public Result<CaptchaVO> refreshCaptcha(@RequestBody Map<String, String> request) {
        String type = request.getOrDefault("type", "image");
        String oldCaptchaId = request.get("captchaId");
        
        // 清除旧验证码
        if (oldCaptchaId != null) {
            captchaService.clearCaptcha(oldCaptchaId);
        }
        
        // 生成新验证码
        return generateCaptcha(type);
    }
}


